package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.GreFileDao;
import cn.topevery.garden.entity.po.GreFile;
import cn.topevery.garden.entity.query.GreFileQuery;
import cn.topevery.garden.entity.vo.GreFileVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.basis.runtime.UserRuntime;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.entity.SqlUpdateMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import hw.topevery.framework.entity.Value;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 绿地/行道树/古树名木  附件表
 *
 * @author code-generate
 * @date 2022-01-05
 */
@Repository
public class GreFileDaoImpl extends BaseDaoImpl<GreFile> implements GreFileDao {
    @Override
    public <P extends DbCsp> List<GreFile> getSearch(P para) {
        return null;
    }

    

    @Override
    public DataPager<GreFileVO> listPage(GreFileQuery query) {
        String table = "t_gre_file tgf";

        String columns = "tgf.*";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tgf.c_id desc");
        sqlQueryMate
                .where(true, "tgf.c_db_status", ScriptConditionEnum.Eq, 0)
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, GreFileVO.class);
    }

    @Override
    public GreFile getEntityByColumn(String key,String value){
        KeyValue<String> map=new KeyValue<>();
        map.key=key;
        map.value=value;
        return getEntityByColumnEqValue(map);
    }

    @Override
    public List<GreFile> getListByMainId(Integer mainId, Integer dataType) {
        Value<List<GreFile>> res = new Value<>();
        List<KeyValue<?>> parameters = new ArrayList<>();
        StringBuffer sb = new StringBuffer();
        sb.append("select * from t_gre_file  where c_db_status = 0");

        sb.append(" and c_main_id  = :mainId ");
        parameters.add(new KeyValue("mainId", mainId));

        sb.append(" and c_data_type  = :dataType ");
        parameters.add(new KeyValue("dataType", dataType));
        String sqlText = sb.toString();

        getDbExecute().run(sqlText, CommandType.Text, dbCmd -> {
            for (KeyValue parameter : parameters) {
                dbCmd.addInParameter(parameter.key, parameter.value);
            }
            res.data = dbCmd.executeToList(GreFile.class);
        });
        return res.data;
    }

    @Override
    public List<GreFile> getListByMainRecordId(Integer mainId, Integer dataType) {
//        Value<List<GreFile>> res = new Value<>();
//        List<KeyValue<?>> parameters = new ArrayList<>();
//        StringBuffer sb = new StringBuffer();
//        sb.append("select * from t_gre_file  where c_db_status = 0");
//
//        sb.append("and c_main_record_id  = :mainId ");
//        parameters.add(new KeyValue("mainId", mainId));
//
//        sb.append("and c_data_type  = :dataType ");
//        parameters.add(new KeyValue("dataType", dataType));
//        String sqlText = sb.toString();
//
//        getDbExecute().run(sqlText, CommandType.Text, dbCmd -> {
//            for (KeyValue parameter : parameters) {
//                dbCmd.addInParameter(parameter.key, parameter.value);
//            }
//            res.data = dbCmd.executeToList(GreFile.class);
//        });
        SqlQueryMate sqlQueryMate = new SqlQueryMate("t_gre_file tgf").setColumns("tgf.*");
        sqlQueryMate.where(true, "tgf.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, "tgf.c_main_id", ScriptConditionEnum.Eq, mainId)
                .where(true, "tgf.c_data_type", ScriptConditionEnum.Eq,  dataType);
        return selectList(sqlQueryMate, GreFile.class);
    }

    @Override
    public void deleteByMainId(Integer id,String userId,Integer type) {
        String sql = "update t_gre_file set c_db_status = 1  where c_main_id = " + id+" and c_data_type = "+ type;
        run(sql, CommandType.Text, dbCommand -> {
            dbCommand.executeNonQuery();
        });
    }

    @Override
    public void insert(GreFile greFile) {
        insert(UserRuntime.getSysUser().userId, greFile);
    }

    @Override
    public void insertList(List<GreFile> list) {
        insert(UserRuntime.getSysUser().userId, list);
    }

    @Override
    public void deleteByMainId(Integer mainId, Integer userId) {
        SqlUpdateMate sqlUpdateMate = new SqlUpdateMate("t_gre_file");
        sqlUpdateMate.setColumnValue(true, "c_db_status", 1)
                .setColumnValue(true, "c_db_last_update_id", userId)
                .setColumnValue(true, "c_db_last_update_date", LocalDateTime.now())
                .where(true, "c_main_id", ScriptConditionEnum.Eq, mainId);
        run(sqlUpdateMate.getSql(), CommandType.Text, dbCommand -> {
            sqlUpdateMate.getParameters().forEach(dbCommand::addParameter);
            dbCommand.executeNonQuery();
        });
    }

    @Override
    public void deleteByMainIds(List<String> ids, Integer userId) {
        SqlUpdateMate sqlUpdateMate = new SqlUpdateMate("t_gre_file");
        sqlUpdateMate.setColumnValue(true, "c_db_status", 1)
                .setColumnValue(true, "c_db_last_update_id", userId)
                .setColumnValue(true, "c_db_last_update_date", LocalDateTime.now())
                .where(true, "c_main_id", ScriptConditionEnum.In, ids);
        run(sqlUpdateMate.getSql(), CommandType.Text, dbCommand -> {
            sqlUpdateMate.getParameters().forEach(dbCommand::addParameter);
            dbCommand.executeNonQuery();
        });
    }
}
